//logs.js
import request from "../../utils/request";
var api = require("../../utils/api");
import {
  getToken
} from '../../utils/util.js'
const {
  $Message
} = require('../commpoents/iview-dist/base/index.js');
const app = getApp()
Page({
  data: {
    imgUrl: '',
    mode: 'aspectFit',
    haveImg: false,
    amount: 0, // 可提现金额
    getMoney: Number, //提现金额
  },
  onLoad() {
    this.SecretEwm();
    this.SecretAmount();
  },
  changeImg(){
    this.setData({
      haveImg: false
    })
  },
  //提现
  tixian(){
    if (this.data.amount < this.data.getMoney) {
      $Message({
        content: '提现金额不得大于可提现金额',
        type: 'error'
      });
      return
    } else if(this.data.getMoney === 0){
      $Message({
        content: '抱歉您无可提现金额',
        type: 'error'
      });
      return
    }
    var url = api.SecretTixian
    var params = {
      num: this.data.getMoney
    }
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        $Message({
          content: '已发起提现申请',
          type: 'success',
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 100);
      }

    })

  },
  onInput(e) {
    var money = Number(e.detail.value)
    this.setData({
      getMoney: money
    })
  },
  // 全部提现
  getAll() {
    this.setData({
      getMoney: this.data.amount
    })
  },
  // 收款码提交
  SecretEwm() {
    var url = api.SecretEwm;
    var params = {
      img_url: this.data.imgUrl
    }
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        if(res.data.ewm === ''){
          this.setData({
            haveImg: false
          })
        }else{
          debugger
          this.setData({
            imgUrl: res.data.ewm,
            haveImg: true
          })
        }
      } else if (res.code === 1000005) {
        // 无收款码
        this.setData({
          haveImg: false
        })
      }
    })
  },

  // 可提现金额
  SecretAmount() {
    var url = api.SecretAmount;
    var params = {}
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        var resultAmount = res.data.amount;
          this.setData({
            amount: resultAmount
          })
      }
    })
  },

  // 获取图片地址
  fileImg() {
    let self = this;
    var uploadUrl = api.SecretUpload_file
    var token = getToken()
    var openid = app.globalData.loginInfo.openid
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: function(res) {
        wx.uploadFile({
          url: uploadUrl,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'access_token': token,
            'size': 0.1
          },
          success: function(result) {
            var data = JSON.parse(result.data)
            if (data.code === 0) {
              self.setData({
                imgUrl: data.data.file
              })
              self.SecretEwm()
            } else {

            }
          }
        })
      },
    })
  },
})